/**
 * 资金账户管理
 */
app.controller("accountManagerCtr", ["$scope", "fundsAccountService", "eventServiceFactory", function ($scope, fundsAccountService, eventServiceFactory) {

    /**
     * 获取账户管理列表
     */
    $scope.getAccountManagerList = function () {
        var config = {
            'urlParams': {}
        };
        fundsAccountService.getAccountManagerList(config, function (res) {
            if (res.errorCode === 0) {
                $scope.list = res.data || [];
            } else {
                setTimeout(function(){
                    $(document).promptBox({isDelay: true, contentDelay: res.msg, type: 'errer', manualClose: true});
                },0)
            }
        });
    };

    /**
     * 获取资金账户详情
     * @param id
     */
    $scope.getAccountDetail = function (block, item) {
        if (!item.thirdPayAccountId || !item.openAccount) {
            return;
        }
        $scope.$parent.block = block;
        eventHandler.dispatch('accountDetailData', {
            'thirdPayAccountId': item.thirdPayAccountId,
            'custName': item.custName
        });
    };

    /**
     * 开户
     * @param item
     */
    $scope.openAccountManager = function (item) {
        var opt = {
            title: '提示',
            type: 'success',
            content: {
                tip: '确认开通资金存管账户？'
            },
            operation: [
                {
                    type: 'submit',
                    description: '确定',
                    operationEvent: function () {
                        var config = {
                            'seatParams': {
                                'accountId': item.accountId
                            }
                        };
                        fundsAccountService.openAccountManager(config, function (res) {
                            //展示第三方消息
                            res.thirdMsg = (res.data && res.data.rspMsg) ? res.data.rspMsg : res.msg;

                            if (res.errorCode === 0) {
                                $(document).promptBox('closePrompt');
                                $scope.getAccountManagerList();
                                $scope.$apply();
                                $(document).promptBox({isDelay: true, contentDelay: res.thirdMsg, type: 'success'});
                            } else {
                                $(document).promptBox({
                                    isDelay: true,
                                    contentDelay: res.thirdMsg,
                                    type: 'errer',
                                    manualClose: true
                                });
                            }
                        });
                    }
                }
            ]
        };
        $(document).promptBox(opt);
    };

    /**
     * 初始化
     */
    $scope.init = function () {
        $scope.getAccountManagerList();
    };
    $scope.init();    //初始化调用一次
    var eventHandler = eventServiceFactory.createEventService();
    eventHandler.on('accountManagerData', $scope.init);
}]);